<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <div class="acordion">
            <?php header('Content-type: text/html; charset=utf-8'); ?>
            <?php
            $baseUrl = Yii::app()->baseUrl;
            $cs = Yii::app()->getClientScript();
            $cs->registerScriptFile($baseUrl . '/js/lib/jquery-1.6.1.js');
            $cs->registerScriptFile($baseUrl . '/js/jquery-ui-1.8.16.custom.min.js');
            $cs->registerCssFile($baseUrl . '/css/smoothness/jquery-ui-1.8.10.custom.css');
            $cs->registerCssFile($baseUrl . '/js/chosen/chosen.css');
            $cs->registerScriptFile($baseUrl . '/js/chosen/chosen.jquery.min.js');
            $cs->registerScriptFile($baseUrl . '/js/jquery.meio.mask.min.js');
            $cs->registerScriptFile($baseUrl . '/js/jquery.metadata.js');
            $cs->registerScriptFile($baseUrl . '/js/jquery.validate.min.js');
            $cs->registerScriptFile($baseUrl . '/js/egresados_modal.js');
            $cs->registerScriptFile($baseUrl . '/js/validator_egresados.js');
            $cs->registerCssFile($baseUrl . '/css/egresados_modal.css');
            ?>
            <?php

            function obtiene_campo($tipocampo, $tipodato, $idcampo, $idalias, $nombre, $requerido, $help, $logitud, $valores="", $valAdd="", $valmuni="", $valdpt="", $registro="", $idRespuesta="") {
                $classfield = "dinamicmultmodal";
                if ($requerido == 1)
                    $classAdd = " required";

                if (is_numeric($idRespuesta))
                    $tipo = "tipo='U'";
                else 
                    $tipo = "tipo='I'";
            
                if ($idcampo == "93" || $idcampo == "175")
                    $titulo = "titulo";
                else
                    $titulo = "";

                switch ($tipodato) {
                    case 1:
                        $clase = "Ctexto";
                        break;
                    case 2:
                        $clase = "Cnumero";
                        break;
                    case 3:
                        $clase = "Cfecha";
                        break;
                    case 4:
                        $clase = "Ccombo";
                        break;
                }
                $clase = $clase . $classAdd;
                switch ($tipocampo) {
                    case 1:
                        $campo = "<div class='control'><input type='text' id='id_alias_campo_$idalias' name='name_alias_campo_$idalias' class='$classfield $clase $titulo' maxlength='$logitud' value='$valores' title='$help' $tipo aliasid='$idalias' /></div>";
                        break;
                    case 2:
                        $campo = "<div class='control'><textarea rows='2' cols='20' id='id_alias_campo_$idalias' name='name_alias_campo_$idalias' maxlength='$logitud' class='$classfield Ctextarea $titulo' title='$help' $tipo aliasid='$idalias' >$valor</textarea></div>";
                        break;
                    case 3:
                        $campo = "<div class='control'><select id='id_alias_campo_$idalias' name='name_alias_campo_$idalias' class='$classfield $clase' title='$help' $tipo aliasid='$idalias' >";
                        $connection = Yii::app()->db;

                        if ($valdpt == "1")
                            $command = $connection->createCommand("select DPTID, DPTDEPARTAMENTO FROM departamentos order by 2");
                        elseif ($valmuni == "1" && $valAdd != "")
                            $command = $connection->createCommand("select MPOID, MPOMUNICIPIO FROM municipios WHERE DPT_ID=$valAdd order by 2");
                        else
                            $command = $connection->createCommand("select LISTID, LISTNOMBRE FROM listasdesplegables where cmp_id=$idcampo order by 1");

                        if ($valmuni == "1" && $valAdd == "") {
                            $campo .= "<option value=''></option>";
                            $campo .="</select></div>";
                            break;
                        }

                        $results = $command->queryAll();
                        $campo .= "<option value=''></option>";
                        foreach ($results as $rows) {
                            $valor = array_values($rows);
                            if ($valor[0] == $valores)
                                $campo .="<option value='$valor[0]' selected='selected'>$valor[1]</option>";
                            else
                                $campo .="<option value='$valor[0]'>$valor[1]</option>";
                        }
                        $campo .="</select></div>";
                        break;
                    case 4:
                        $campo = "<div class='control'><select multiple='multiple' id='id_alias_campo_$idalias' name='name_alias_campo_$idalias' class='$classfield $clase' title='$help' $tipo aliasid='$idalias' >";
                        $connection = Yii::app()->db;
                        $command = $connection->createCommand("select LISTID, LISTNOMBRE from listasdesplegables where cmp_id=$idcampo order by 1");
                        $results = $command->queryAll();
                        foreach ($results as $rows) {
                            $valor = array_values($rows);
                            //$valor DEFINIR VALORES SELECCIONADOS
                            $MultVal = explode(",", $valores);
                            if (is_numeric(array_search($valor[0], $MultVal)))
                                $campo .="<option value='$valor[0]' selected='selected'>$valor[1]</option>";
                            else
                                $campo .="<option value='$valor[0]'>$valor[1]</option>";
                        }
                        $campo .="</select></div>";
                        break;
                    case 5:
                        $campo = "<div id='id_alias_campo_$idalias' class='$classfield Cradios' title='$help' $tipo aliasid='$idalias' fielddiv='true' >";
                        $connection = Yii::app()->db;
                        $command = $connection->createCommand("select LISTID, LISTNOMBRE from listasdesplegables where cmp_id=$idcampo order by 1");
                        $results = $command->queryAll();
                        if ($requerido == 1)
                            $addreqRadio = "validate=\"required:true\"";

                        foreach ($results as $rows) {
                            $valor = array_values($rows);
                            $campo .= "<div class='radio'>";
                            if ($valor[0] == $valores)
                                $campo .= "<input type='radio' class='inputradio' id='id_radios_alias_campo_$idalias-$valor[0]' name='name_alias_campo_$idalias' value='$valor[0]' checked='checked' $addreqRadio /><label class='labelradio' for ='id_alias_campo_$idalias-$valor[0]'>$valor[1]</label>";
                            else
                                $campo .= "<input type='radio' class='inputradio' id='id_radios_alias_campo_$idalias-$valor[0]' name='name_alias_campo_$idalias' value='$valor[0]'  $addreqRadio /><label class='labelradio' for ='id_alias_campo_$idalias-$valor[0]'>$valor[1]</label>";

                            $campo .= "</div>";
                        }
                        $campo .="</div>";
                        break;
                    case 6:
                        $campo = "<div id='id_alias_campo_$idalias' class='$classfield Cchecked' title='$help' $tipo aliasid='$idalias' fielddiv='true' >";
                        $connection = Yii::app()->db;
                        $command = $connection->createCommand("select LISTID, LISTNOMBRE from listasdesplegables where cmp_id=$idcampo order by 1");
                        $results = $command->queryAll();
                        foreach ($results as $rows) {
                            $valor = array_values($rows);

                            if ($requerido == 1)
                                $addreqRadio = "validate=\"required:true\"";

                            //$valor DEFINIR VALORES SELECCIONADOS
                            $MultVal = explode(",", $valores);
                            $campo .= "<div class='checked'>";
                            if (is_numeric(array_search($valor[0], $MultVal)))
                                $campo .= "<input class='input inputchecked' type='checkbox' id='id_checks_alias_campo_$idalias-$valor[0]' name='name_alias_campo_$idalias-$valor[0]' value='$valor[0]' checked='checked' $addreqRadio /><label class='labelchecked' for ='id_alias_campo_$idalias-$valor[0]'>$valor[1]</label>";
                            else
                                $campo .= "<input class='input inputchecked' type='checkbox' id='id_checks_alias_campo_$idalias-$valor[0]' name='name_alias_campo_$idalias-$valor[0]' value='$valor[0]' $addreqRadio /><label  class='labelchecked' for ='id_alias_campo_$idalias_$valor[0]'>$valor[1]</label>";

                            $campo .= "</div>";
                        }
                        $campo .="</div>";
                        break;
                }
                $label = "<div class='label'><label for='id_alias_campo_$idalias' class='label_info' >$nombre</label></div>";
                return $label . $campo;
            }

            if (is_numeric($id) && is_numeric($grp) && is_numeric($filter)) {
                $sql = "select ca.cmpid, ca.tcmp_id, ca.tdt_id, al.alsid, al.alsnombre, al.alsayuda, al.alsobligatorio, al.alsorden, 
                ca.CMP_LONGITUD, ca.CMPMUNICIPIO, ca.CMPDEPARTAMENTO, ca.GRP_ID, 
                (select resmdescripcionmin from respuestasmultiples where als_id=al.alsid and resmregistro=$filter and GRP_ID = ca.grp_id and ENR_ID = $id ) as resmdescripcionmin, 
                $filter as RESMREGISTRO, (select RESMID from respuestasmultiples where als_id=al.alsid and resmregistro=$filter and GRP_ID = ca.grp_id and ENR_ID = $id ) as RESMID from campos ca
                join alias al on ca.cmpid=al.cmp_id 
                WHERE ca.GRP_ID=$grp
                order by al.alsorden";

                $connection = Yii::app()->db;
                $command = $connection->createCommand($sql);
                $results = $command->queryAll();
                $regActual = "";
                $ModalCampos = "";
                $titulo = "";
                //INICIO CREACION CAMPOS
                foreach ($results as $rows) {
                    $valor = array_values($rows);
                    $reg = $valor[13];

                    if ($regActual == "")
                        $regActual = $reg;

                    if ($reg != $regActual) {
                        echo "\n<h3 id ='h3_$regActual'><a href='#'>$titulo</a></h3>\n<div id='registro_$regActual' class='registros'>\n<form id='form_modal_multiple_$regActual' class='validatemultimodal' method='get' action='#' grupo='$grp' registro='$regActual' >\n" . $ModalCampos;
                        echo "\n<div class='botonmodal'><input type='submit' class='submit' id='guardar_$regActual' name='guardar_$regActual' value='Actualizar Registro' />\n";
                        echo "\n<input type='button' class='submit eliminarmodal' id='eliminar_$regActual' name='eliminar_$regActual' value='Eliminar Registro' grupo='$grp' registro='$regActual' /></div>\n";
                        echo "\n</form></div>\n";
                        $ModalCampos = "";
                        $regActual = "";
                        $titulo = "";
                    }

                    if ($titulo == "")
                        $titulo = $valor[12];

                    if ($valor[10] == "1" && $valor[12] != "")
                        $valAdd = $valor[12];
                    elseif ($valor[9] != "1")
                        $valAdd = "";

                    $ModalCampos .= "\n<div>" . obtiene_campo($valor[1], $valor[2], $valor[0], $valor[3], $valor[4], $valor[6], $valor[5], $valor[8], $valor[12], $valAdd, $valor[9], $valor[10], $reg, $valor[14]) . "</div>\n";
                }
                if ($ModalCampos != "") {
                    echo "\n<h3 id ='h3_$regActual'><a href='#'>$titulo</a></h3>\n<div id='registro_$reg' class='registros'>\n<form id='form_modal_multiple_$reg' class='validatemultimodal' method='get' action='#' grupo='$grp' registro='$reg' >\n" . $ModalCampos;
                    echo "\n<div class='botonmodal'><input type='submit' class='submit' id='guardar_$reg' name='guardar_$reg' value='Actualizar Registro' />\n";
                    echo "\n<input type='button' class='submit eliminarmodal' id='eliminar_$reg' name='eliminar_$reg' value='Eliminar Registro' grupo='$grp' registro='$reg' /></div>\n";
                    echo "\n</form></div>\n";
                    $ModalCampos = "";
                    $regActual = "";
                    $titulo = "";
                }
            }
            ?>
        </div>
        <?php
        echo "<div><input id='ecr_id' type='hidden' value='$id'></div>";
        ?>
        <div class="upprogress">
            <img alt="Cargando..." src="images/loading.gif" />
        </div>
        <div class="dialog-message">
            <p class="dialog-content">
            </p>
        </div>
    </body>
</html>